package com.athuawei.blockqueue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * 阻塞对列：
 * 1个线程往队列里放元素，1个线程从多列取元素；
 * (1)ArrayBlockingQueue
 * (2)LinkedBlockingQueue
 * (3)DelayQueue
 * (4)PriorityBlocingQueue
 * (5)SynchronousQueue
 * <p>
 * 抛出异常：add(e); remove(); element();
 * 特殊值：offer(e); poll(); peek();
 * <p>
 * 阻塞：put(e); take();
 * <p>
 * 超时：offer(e,time,unit)
 * poll(time,unit)
 *
 * 自动触发阻塞put线程和get线程
 */
public class BlockQueueDemo {

    public static void main(String[] args) throws InterruptedException {
        // 1、创建阻塞队列
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);

        //2. 第一组
//        System.out.println(blockingQueue.add("redis"));
//        System.out.println(blockingQueue.add("juc"));
//        System.out.println(blockingQueue.add("jvm"));
//        System.out.println(blockingQueue.element());

//        System.out.println(blockingQueue.add("jdk"));
//        System.out.println(blockingQueue.remove());
//        System.out.println(blockingQueue.remove());
//        System.out.println(blockingQueue.remove());
//        System.out.println(blockingQueue.remove());

        // 第二组
//        System.out.println(blockingQueue.offer("spring"));
//        System.out.println(blockingQueue.offer("boot"));
//        System.out.println(blockingQueue.offer("cloud"));
//        System.out.println(blockingQueue.offer("elasticsearch"));
//
//        System.out.println(blockingQueue.poll());
//        System.out.println(blockingQueue.poll());
//        System.out.println(blockingQueue.poll());
//        System.out.println(blockingQueue.poll());

        //第三组
//        blockingQueue.put("hash");
//        blockingQueue.put("list");
//        blockingQueue.put("set");
//        blockingQueue.put("array");
//        System.out.println(blockingQueue.take());
//        System.out.println(blockingQueue.take());
//        System.out.println(blockingQueue.take());
//        System.out.println(blockingQueue.take());

        //第四组
        System.out.println(blockingQueue.offer("tree"));
        System.out.println(blockingQueue.offer("node"));
        System.out.println(blockingQueue.offer("map"));
        System.out.println(blockingQueue.offer("string", 3L, TimeUnit.SECONDS));
    }
}
